package com.wcl.nullmusic.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.Response;
import com.google.gson.Gson;
import com.wcl.nullmusic.R;
import com.wcl.nullmusic.base.BaseActivity;
import com.wcl.nullmusic.base.BaseAppcation;
import com.wcl.nullmusic.mode.RankingResult;
import com.wcl.nullmusic.mode.Recommend_CategoryContents_List_List;
import com.wcl.nullmusic.ui.adapter.RankingRecyclerViewAdapter;
import com.wcl.nullmusic.utils.Contant;
import com.wcl.nullmusic.utils.ProgressBarUtils;
import com.wcl.nullmusic.utils.ToActivityUtils;
import com.wcl.nullmusic.utils.UrlUtils;
import com.wcl.nullmusic.utils.VolleyUtils;
import com.wcl.nullmusic.view.RefreshRecyclerView;

import java.util.List;

/**
 * Created by dllo on 16/1/17.
 */
public class RankingActivity extends BaseActivity implements RefreshRecyclerView.OnLoadDataListener, SwipeRefreshLayout.OnRefreshListener {
    private CollapsingToolbarLayout ranking_collapsing;
    private Toolbar ranking_toolbar;
    private RefreshRecyclerView ranking_recyclerView;
    private String key;
    private String title;
    private RankingRecyclerViewAdapter adapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    private int position = 2;
    private List<Recommend_CategoryContents_List_List> datas;
    private ImageView ranking_white;
    private boolean isShowProgressDialog = true;

    @Override
    protected void conService() {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_ranking;
    }

    @Override
    protected void getVaule() {
        Bundle bundle = ToActivityUtils.fromSrc(this);
        if (bundle != null) {
            this.key = bundle.getString("key");
            this.title = bundle.getString("title");
        }
    }

    @Override
    protected void initView() {
        BaseAppcation.getInstance().addActivity(this);
        ProgressBarUtils.showProgressDialog(this);
        swipeRefreshLayout = findView(R.id.ranking_swipeRefreshLayout);
        ranking_collapsing = findView(R.id.ranking_collapsing);
        ranking_toolbar = findView(R.id.ranking_toolbar);
        ranking_recyclerView = findView(R.id.ranking_recyclerView);
        adapter = new RankingRecyclerViewAdapter(this);
        ranking_white = findView(R.id.ranking_white);
    }

    @Override
    protected void initData() {
        setSupportActionBar(ranking_toolbar);
        ranking_collapsing.setTitle("音乐榜TOP50");
        ranking_collapsing.setExpandedTitleColor(Color.GREEN);
        ranking_collapsing.setCollapsedTitleTextColor(Color.WHITE);
        ranking_recyclerView.setLayoutManager(new LinearLayoutManager(this));
        ranking_recyclerView.setAdapter(adapter);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_light, android.R.color.holo_blue_light, android.R.color.holo_green_light);
        ranking_recyclerView.setItemAnimator(new DefaultItemAnimator());
        ranking_recyclerView.setLoadMoreEnable(true);
        ranking_recyclerView.setFooterResource(R.layout.recyclerview_load_footer);
        loadData();
    }

    @Override
    protected void listener() {
        ranking_recyclerView.setOnLoadDataListener(this);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

//    private void getRecyclerviewLoadFooterView() {
//        View view = LayoutInflater.from(this).inflate(R.layout.recyclerview_load_footer, null);
//
//    }

    private void loadData() {
        VolleyUtils.getInstance().StringRequest(Contant.RANKING_PREFIX_URL + UrlUtils.toEncoder(key) + Contant.RANKING_MIDDLE_URL
                + UrlUtils.toEncoder(title), new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                RankingResult result = gson.fromJson(response, RankingResult.class);
                datas = result.getList();
                adapter.addData(datas);
                ranking_recyclerView.notifyNewData();
                if (isShowProgressDialog) {
                    isShowProgressDialog = false;

                    ProgressBarUtils.dismissProgressDialog();
                    ranking_white.setVisibility(View.GONE);
                }
            }
        }, "Ranking_recyclerView_StringRequest");
    }

    @Override
    public void pullUpRefresh() {
        VolleyUtils.getInstance().StringRequest(Contant.RANKING_LOAD_PREFIX_URL + UrlUtils.toEncoder(key) + Contant.RANKING_LOAD_MIDDLE_URL
                + position + Contant.RANKING_LOAD_SUFDLE_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                position++;
                Gson gson = new Gson();
                RankingResult result = gson.fromJson(response, RankingResult.class);
                datas.addAll(result.getList());
                Log.i("rf", result.getList().size() + "<<<");
                adapter.addData(datas);
                ranking_recyclerView.notifyMoreData();//刷新数据
                if (position == 3)
                    ranking_recyclerView.setLoadMoreEnable(false);
            }
        }, "Ranking_recyclerView_StringRequest");

    }

    @Override
    public void onRefresh() {
        ranking_recyclerView.setLoadMoreEnable(true);
        position = 2;
        swipeRefreshLayout.setRefreshing(false);
        loadData();
        ranking_recyclerView.notifyNewData();//刷新数据
    }

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 200:
//                    recycler_load_footer_progress.setVisibility(View.GONE);
//                    recycler_load_footer_loading.setText("没有更多数据");
                    break;
                case 300:
                    break;
            }
            return false;
        }
    });

    @Override
    protected void onDestroy() {
        BaseAppcation.getInstance().finishActivity(this);
        super.onDestroy();
    }
}
